package cn.edu.sdjzu.xg.bysj.dao;


import cn.edu.sdjzu.xg.bysj.domain.Department;
import cn.edu.sdjzu.xg.bysj.domain.StudentClass;
import cn.edu.sdjzu.xg.bysj.exception.BysjException;
import cn.edu.sdjzu.xg.bysj.service.DepartmentService;
import cn.edu.sdjzu.xg.bysj.util.JdbcHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.TreeSet;

public final class StudentClassDao {

    private static StudentClassDao studentClassDao = new StudentClassDao();

    private StudentClassDao() {
    }

    public static StudentClassDao getInstance() {
        if (studentClassDao == null) {
            studentClassDao = new StudentClassDao();
        }
        return studentClassDao;
    }

    public boolean add(StudentClass studentClass) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        //准备预编译的语句
        String sql_insert = "INSERT INTO studentClass " +
                "(description, no, department_id) VALUES ( ?, ?, ?)";
        PreparedStatement pstmt_update =
                connection.prepareStatement(sql_insert);

        pstmt_update.setString(1, studentClass.getDescription());
        pstmt_update.setString(2, studentClass.getNo());
        pstmt_update.setInt(3, studentClass.getDepartment().getId());

        //执行预编译语句，影响的行数保存在rowAffected中
        int rowAffected = pstmt_update.executeUpdate();

        JdbcHelper.close(pstmt_update, connection);
        return rowAffected > 0;
    }

    public StudentClass find(Integer id) throws SQLException, BysjException {
        //创建StudentClass类型的引用，暂不创建对象
        StudentClass desiredStudentClass = null;
        //获得数据库连接
        Connection connection = JdbcHelper.getConn();
        String sql_select_id = "SELECT * FROM student_class WHERE id = ?";
        PreparedStatement pstmt_select_id =
                connection.prepareStatement(sql_select_id);
        pstmt_select_id.setInt(1, id);
        ResultSet resultSet_select_id = pstmt_select_id.executeQuery();
        //如果表中存在id对应的记录，则获得表中的字段值，并创建对象
        if (resultSet_select_id.next()) {
            String description = resultSet_select_id.getString("description");
            String no = resultSet_select_id.getString("no");
            int departmentId = resultSet_select_id.getInt("department_id");
            //获得当前StudentClass隶属的学院
            Department department = DepartmentService.getInstance().find(departmentId);
            desiredStudentClass = new StudentClass(id, description, no, department);
        }
        //关闭三个资源
        JdbcHelper.close(resultSet_select_id, pstmt_select_id, connection);
        return desiredStudentClass;
    }

    public Collection<StudentClass> findAll() throws SQLException, BysjException {
        //创建集合类对象，用来保存所有记录代表的StudentClass对象
        //TreeSet按compare(Object o)方法指定的顺序排序
        Collection<StudentClass> desiredStudentClasss = new TreeSet<StudentClass>();
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        //准备预编译的语句
        String sql_select_all = "SELECT * FROM StudentClass";
        PreparedStatement pstmt_select_all =
                connection.prepareStatement(sql_select_all);
        //执行预编译语句，结果集保存在resultSet中
        ResultSet resultSet_select_all = pstmt_select_all.executeQuery();
        //遍历resultSet
        while (resultSet_select_all.next()) {
            //获得studentClass表中当前记录的id字段的值
            int id = resultSet_select_all.getInt("id");
            String description = resultSet_select_all.getString("description");
            String no = resultSet_select_all.getString("no");
            int departmentId = resultSet_select_all.getInt("department_id");
            //获得当前StudentClass隶属的系
            Department department = DepartmentService.getInstance().find(departmentId);
            StudentClass desiredStudentClass =
                    new StudentClass(id, description, no, department);
            //将当前记录代表的StudentClass对象添加到desiredStudentClasss中
            desiredStudentClasss.add(desiredStudentClass);
        }
        JdbcHelper.close(resultSet_select_all, pstmt_select_all, connection);
        return desiredStudentClasss;
    }

    public boolean update(StudentClass studentClass) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        //准备预编译的语句
        String sql_update =
                "UPDATE studentClass SET description = ?, no = ?, department_id = ? " +
                        "WHERE id = ?";
        PreparedStatement pstmt_update =
                connection.prepareStatement(sql_update);

        pstmt_update.setString(1, studentClass.getDescription());
        pstmt_update.setString(2, studentClass.getNo());
        pstmt_update.setInt(3, studentClass.getDepartment().getId());
        pstmt_update.setInt(4, studentClass.getId());

        //执行预编译语句，影响的行数保存在rowAffected中
        int rowAffected = pstmt_update.executeUpdate();

        JdbcHelper.close(pstmt_update, connection);
        return rowAffected > 0;
    }

    public boolean delete(Integer id) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        //准备预编译的语句
        String sql_delete_id = "DELETE FROM studentClass WHERE id = ?";
        PreparedStatement pstmt_delete =
                connection.prepareStatement(sql_delete_id);

        pstmt_delete.setInt(1, id);

        //执行预编译语句，影响的行数保存在rowAffected中
        int rowAffected = pstmt_delete.executeUpdate();

        JdbcHelper.close(pstmt_delete, connection);
        return rowAffected > 0;
    }

    public boolean delete(StudentClass studentClass) throws SQLException {
        return this.delete(studentClass.getId());
    }

    public int count(Department department) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        //准备预编译的语句
        String sql_count_by_department_id = "SELECT COUNT(id) as  cnt_by_department_id FROM studentClass WHERE department_id = ?";

        PreparedStatement pstmt_count_by_department_id =
                connection.prepareStatement(sql_count_by_department_id);

        pstmt_count_by_department_id.setInt(1, department.getId());

        int count_by_department_id = 0;
        //执行预编译语句，结果集保存在resultSet中
        ResultSet resultSet_count_by_department_id = pstmt_count_by_department_id.executeQuery();
        if (resultSet_count_by_department_id.next()) {
            count_by_department_id = resultSet_count_by_department_id.getInt("cnt_by_department_id");
        }
        JdbcHelper.close(resultSet_count_by_department_id, pstmt_count_by_department_id, connection);
        return count_by_department_id;
    }


}

